﻿using NPOI.SS.Formula.Functions;
using System;
using System.IO;
using System.Text;

namespace tablegen2.logic
{
    public static class TableExcelExportDat
    {
        public static void exportExcelFile(TableExcelData data, string filePath)
        {
            const int version = 1;

            var ms = new MemoryStream();
            var bw = new BinaryWriter(ms, Encoding.UTF8);
            bw.Write(version);

            foreach (var hdr in data.Headers)
            {
                bw.WriteUtf8String(hdr.FieldName);
                byte ftype = 0;
                switch (hdr.FieldType)
                {
                    case "int":
                        ftype = 1;
                        break;
                    case "float":
                        ftype = 2;
                        break;
                    case "string":
                        ftype = 3;
                        break;
                    case "long":
                        ftype = 4;
                        break;
                    case "string[]":
                        ftype = 5;
                        break;
                    case "int[]":
                        ftype = 6;
                        break;
                    case "long[]":
                        ftype = 7;
                        break;
                    default:
                        throw new Exception(string.Format("无法识别的字段类型 {0} 名称 {1}", hdr.FieldType, hdr.FieldName));
                }
                bw.Write(ftype);
            }
            bw.Write((byte)0);

            foreach (var row in data.Rows)
            {
                for (int i = 0; i < data.Headers.Count; i++)
                {
                    var hdr = data.Headers[i];
                    var val = row.StrList[i];
                    switch (hdr.FieldType)
                    {
                        case "int":
                            {
                                int n = 0;
                                int.TryParse(val, out n);
                                bw.Write(n);
                            }
                            break;
                        case "float":
                            {
                                float n = 0;
                                float.TryParse(val, out n);
                                bw.Write(n);
                            }
                            break;
                        case "string":
                            string str = "";
                            if (!string.IsNullOrEmpty(val))
                                str = val;
                            bw.WriteUtf8String(str);
                            break;
                        case "long":
                            {
                                long n = 0;
                                long.TryParse(val, out n);
                                bw.Write(n);
                            }
                            break;
                        case "string[]":
                            {
                                
                                if (string.IsNullOrEmpty(val))
                                {
                                    bw.Write("");
                                }
                                else
                                {
                                    var numStrs = val.Split(';');
                                    bw.Write(numStrs.Length);
                                    for (int j = 0; j < numStrs.Length; j++)
                                    {
                                        if (string.IsNullOrEmpty(numStrs[j]))
                               
                                            bw.Write("");
                                    
                                        else
                                            bw.WriteUtf8String(numStrs[j]);
                                    }
                                }

                            }
                            break;
                        case "int[]":
                            {

                                if (string.IsNullOrEmpty(val))
                                {
                                    bw.Write(Convert.ToInt32(0));
                                }
                                else
                                {
                                    var numStrs = val.Split(';');
                                    bw.Write(numStrs.Length);
                                    for (int j = 0; j < numStrs.Length; j++)
                                    {
                                        if (string.IsNullOrEmpty(numStrs[j]))
                                            bw.Write(Convert.ToInt32(0));
                                        else
                                            bw.Write(Convert.ToInt32(numStrs[j]));
                                    }
                                }

                                //long n = 0;
                                //long.TryParse(val, out n);
                                //bw.Write(n);
                            }
                            break;
                        case "long[]":
                            {

                                if (string.IsNullOrEmpty(val))
                                {
                                    bw.Write(Convert.ToInt64(0));
                                }
                                else
                                {
                                    var numStrs = val.Split(';');
                                    bw.Write(numStrs.Length);
                                    for (int j = 0; j < numStrs.Length; j++)
                                    {
                                        if (string.IsNullOrEmpty(numStrs[j]))
                                            bw.Write(Convert.ToInt64(0));
                                        else
                                            bw.Write(Convert.ToInt64(numStrs[j]));
                                    }
                                }

                                //long n = 0;
                                //long.TryParse(val, out n);
                                //bw.Write(n);
                            }
                            break;
                    }
                }
            }
            
            if (File.Exists(filePath))
                File.Delete(filePath);

            File.WriteAllBytes(filePath, GzipHelper.processGZipEncode(ms.GetBuffer(), (int)ms.Length));

            bw.Close();
            ms.Close();
        }
    }
}
